{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结构化存储\n",
    "- xml， json\n",
    "## xml文件\n",
    "- 参考资料\n",
    "    - www.runoob.com/python/python-xml.html\n",
    "- xml,可扩展标记语言\n",
    "    - 标记语言：语言中使用尖括号括起来的文本字符串标记\n",
    "    - 可扩展：用户可以自己定义需要的标记\n",
    "    - 例如：\n",
    "    \n",
    "        <Teacher>\n",
    "            自定义标记Teacher\n",
    "            在两个标记之间任何内容都应该跟Teacher相关\n",
    "        </Teacher>\n",
    "- 是w3c组织制定的一个标准\n",
    "- XML描述的是数据本身，即数据的结构和语义\n",
    "- HTML侧重于如何显示web页面中的数据\n",
    "\n",
    "- XML文档的构成：\n",
    "    - 处理指令（可以认为一个文件内只有一个处理指令）\n",
    "        - 最多只有一行\n",
    "        - 且必须在第一行\n",
    "        - 内容是与xml本身处理相关的一些声明或者指令\n",
    "        - 以xml关键字开头，\n",
    "            - 一般用于声明xml本身的版本和编码\n",
    "            - version属性是必须的\n",
    "            - encoding属性用来指出xml解释器使用的编码\n",
    "    - 根元素（一个文件内只有一个根元素）\n",
    "        - 在整个xml文件中，可以把它看作一个树形结构\n",
    "        - 根元素有且只能有一个\n",
    "    - 子元素\n",
    "    - 属性\n",
    "    - 内容\n",
    "        - 表明标签的信息\n",
    "    - 注释\n",
    "        - 起说明作用的信息\n",
    "        - 注释不能嵌套在标签中\n",
    "        - 只有在注释的开头和结尾使用双短横线\n",
    "        - 三短横线只能出现在注释的开头而不能用在结尾\n",
    "        \n",
    "                - <name> <!-- zhushi --> </name> # 可以\n",
    "                - <name <!-- zhushi -->> </name> # 不可以，注释不能在标签中\n",
    "                \n",
    "                - <!--my-name-by-zhushi--># 可以，注释内容可以有一个短横线\n",
    "                - <!--my--name--by--zhushi--># 不可以，双横线只能出现在开头和结尾\n",
    "                \n",
    "                - <!---my-name--> # 可以，三横线可以出现在开头\n",
    "                - <!---my-name---> # 不可以，三横线只可以出现在开头，不能出现在结尾\n",
    "- 保留字符的处理：\n",
    "    - XML中使用的符号可能跟实际符号相冲突，典型的就是<>\n",
    "    - 使用实体引用，来表示保留自负\n",
    "        \n",
    "        <score> score>80 </score> # 有错误，xml中不能出现>\n",
    "        <score> score&gt;80</score> # 使用实体引用\n",
    "    - 把含有保留字符的部分放在CDATA块内部，CDATA块把内部信息视为不需要转义\n",
    "        \n",
    "        <![CDATA[\n",
    "            select name,age\n",
    "            from Student\n",
    "            where score>90\n",
    "            ]]>\n",
    "    - 常用的需要转义的保留字符和对应实体引用\n",
    "        - & : &amp;\n",
    "        - < : &lt;\n",
    "        - > : &gt;\n",
    "        - ' : &apos;\n",
    "        - \" : &quot\n",
    "- XML标签的命名规则\n",
    "    - Pascal命名法\n",
    "    - 用单词表示，第一个字母大写\n",
    "    - 大小写严格区分\n",
    "    - 配对的标签必须一致\n",
    "- 命名空间\n",
    "    - 为了防止命名冲突，需要给可能产生冲突的元素添加命名空间\n",
    "    - xmlns：xml name space 的缩写\n",
    "        - <School xmlns:student=\"http://my_student\" xmlns:room=\"http://my_room\">\n",
    "    <student:Name>Cheng</student:Name>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-c375e685f699>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-1-c375e685f699>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    <?xml version=\"1.0\" encoding=\"utf-8\" ?>\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n",
    "<Student type=\"Web\", loc=\"beijing\">\n",
    "    <name>haha</name>\n",
    "    <age>18</age>\n",
    "</Student>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# XML访问\n",
    "## 读取\n",
    "- XML读取分两个主要技术，SAX，DOM\n",
    "- SAX（Simple API for XML)\n",
    "    - 基于事件驱动的API\n",
    "    - 利用SAX解析文档设计到解析器和事件处理两部分\n",
    "    - 特点：\n",
    "        - 快\n",
    "        - 流式读取\n",
    "        \n",
    "- DOM\n",
    "    - 是w3c规定的XML编程接口\n",
    "    - 一个XML文件在缓存中以树形结构保存，读取\n",
    "    - 用途：\n",
    "        - 定位浏览XML任何一个节点信息\n",
    "        - 添加删除相应内容\n",
    "    - minidom\n",
    "    \n",
    "    - etree\n",
    "## 文件写入\n",
    "- 更改\n",
    "    - ele.set:修改属性\n",
    "    - ele.append：添加子元素\n",
    "    - ele.remove：删除元素\n",
    "- 生成创建\n",
    "    - SubElement\n",
    "    - minidom写入\n",
    "    - etree创建\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
